﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XsqTech.SmartOA.Domain.Table;
using XsqTech.SmartOA.Dtos.DTO;
using XsqTech.SmartOA.Dtos.XSPDto.FormDto;
using XsqTech.SmartOA.Dtos.XSPDto.FromDto;
using XsqTech.SmartOA.EFCore.Respository;
using XsqTech.SmartOA.IRespository;
using XsqTech.SmartOA.IService;
using XsqTech.SmartOA.Service.DTO;

namespace XsqTech.SmartOA.Service
{
    public class MenuService:IMenuService
    {
        IMenuRespository _baseMenu;
        readonly IMapper _mapper;
        readonly IUnitOfWork _unitOfWork;
        private readonly ILogger<MenuService> _logger;

        public MenuService(IMenuRespository baseMenu, IMapper mapper, IUnitOfWork unitOfWork, ILogger<MenuService> logger)
        {
            _baseMenu = baseMenu;
            _mapper = mapper;
            _unitOfWork = unitOfWork;
            _logger = logger;
        }

        /// <summary>
        /// 递归
        /// </summary>
        /// <returns></returns>
        public ResponseModel MenuRecursion()
        {
            var id = Guid.Empty;
            var list = _baseMenu.Query().Where(x => x.DeleteId == 0);
            var data = list.OrderBy(x=>x.SortNo).ToList();
            var m = MenuRecursionChildren(data,id);
            return new ResponseModel
            {
                Data = m,
                Message = "请求成功",
            };
        }
        /// <summary>
        /// 递归子级
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private List<MenuDto> MenuRecursionChildren(List<Menu> data,Guid id)
        {
            var list = data.Where(x => x.PId.Equals(id)).ToList();
            List<MenuDto> Children = new List<MenuDto>();

            foreach (var item in list)
            {
                if (data.Where(s => s.PId == item.Id).Count() > 0)
                {
                    Children.Add(new MenuDto
                    {
                        MenuName = item.MenuName,
                        Id = item.Id,
                        Icon = item.Icon,
                        Index = item.Index,
                        PId = item.PId,
                        DeleteId = item.DeleteId,
                        children = MenuRecursionChildren(data, item.Id),
                    });
                }
                else
                {
                    Children.Add(new MenuDto
                    {
                        MenuName = item.MenuName,
                        Id = item.Id,
                        Icon = item.Icon,
                        Index = item.Index,
                        PId = item.PId,
                        DeleteId = item.DeleteId,
                    });
                }
               
            }

            return Children;

        }
        /// <summary>
        /// 菜单添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ResponseModel AddMenu(MenuDto dto)
        {
            try
            {
                int Count = _baseMenu.Query().Count();
                dto.SortNo = Count + 1;
                //判断表单名称是否重复
                var list = _baseMenu.Query().Where(x => x.MenuName == dto.MenuName);
                if (list.Count() > 0)
                {
                    return new ResponseModel
                    {
                        Code = State.请求失败,
                        Message = "表单名称已从在",
                    };
                }
                var s = _mapper.Map<Menu>(dto);
                _baseMenu.Add(s);
                int data = _unitOfWork.SaveChangs();
                if (data > 0)
                {
                    return new ResponseModel
                    {
                        Code = State.请求成功,
                        Message = "添加成功",
                        Count = data,
                    };
                }
                else
                {
                    return new ResponseModel
                    {
                        Code = State.请求失败,
                        Message = "添加失败",
                        Count = data,
                    };
                }

            }
            catch (Exception ex)
            {
                //控制台输出
                Console.WriteLine(ex.Message);
                //写入日志
                _logger.LogError(ex.Message);
                throw;
            };
        }
        /// <summary>
        /// 菜单显示
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ResponseModel MenuQuery(string Name)
        {
            var list = _baseMenu.Query().Where(x=>x.DeleteId.Equals(0));
            if (!string.IsNullOrEmpty(Name))
            {
                list = list.Where(x => x.MenuName.Contains(Name));
            }

            return new ResponseModel
            {
                Code = State.请求成功,
                Message = "查询成功",
                Data = list,
            };
        }
        /// <summary>
        /// 菜单删除（单删）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseModel MenuDelete(string id)
        {
            try
            {
                var list = _baseMenu.Query().FirstOrDefault(x => x.Id.ToString().Equals(id));
                list.DeleteId = 1;
                _baseMenu.Update(list) ;
                int data = _unitOfWork.SaveChangs();
                if (data > 0)
                {
                    return new ResponseModel
                    {
                        Code = State.请求成功,
                        Message = "删除成功",
                        Count = data,
                    };
                }
                else
                {
                    return new ResponseModel
                    {
                        Code = State.请求失败,
                        Message = "删除失败",
                        Count = data,
                    };
                }
            }
            catch (Exception ex)
            {
                //控制台输出
                Console.WriteLine(ex.Message);
                //写入日志
                _logger.LogError(ex.Message);
                throw;
            };
        }
        /// <summary>
        /// 菜单删除（批删）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ResponseModel AllMenuDelete(List<MenuDto> dto)
        {
            try
            {
                foreach (var item in dto)
                {
                    var list = _baseMenu.Query().FirstOrDefault(x => x.Id == item.Id);
                    if (list != null)
                    {
                        list.DeleteId = 1;
                        _baseMenu.Update(list);
                    }
                    else
                    {
                        return new ResponseModel
                        {
                            Code = State.请求失败,
                            Message = "删除失败",
                        };
                    }
                }
                int data = _unitOfWork.SaveChangs();
                if (data > 0)
                {
                    return new ResponseModel
                    {
                        Code = State.请求成功,
                        Message = "删除成功",
                        Count = data,
                    };
                }
                else
                {
                    return new ResponseModel
                    {
                        Code = State.请求失败,
                        Message = "删除失败",
                        Count = data,
                    };
                }
            }
            catch (Exception ex)
            {
                //控制台输出
                Console.WriteLine(ex.Message);
                //写入日志
                _logger.LogError(ex.Message);
                throw;
            };

        }
        /// <summary>
        /// 菜单反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseModel MenuQueryId(string id)
        {
            var list = _baseMenu.Query().FirstOrDefault(x => x.Id.ToString().Equals(id));
            return new ResponseModel
            {
                Code = State.请求成功,
                Message = "请求成功",
                Data = list,
            };
        }
        /// <summary>
        /// 菜单修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ResponseModel UpdateMenu(MenuDto dto)
        {
            try
            {
                var s = _mapper.Map<Menu>(dto);
                _baseMenu.Update(s);
                int data = _unitOfWork.SaveChangs();
                if (data > 0)
                {
                    return new ResponseModel
                    {
                        Code = State.请求成功,
                        Message = "修改成功",
                        Count = data,
                    };
                }
                else
                {
                    return new ResponseModel
                    {
                        Code = State.请求失败,
                        Message = "修改失败",
                        Count = data,
                    };
                }

            }
            catch (Exception ex)
            {
                //控制台输出
                Console.WriteLine(ex.Message);
                //写入日志
                _logger.LogError(ex.Message);
                throw;
            };
        }
    }
}
